package com.mastercard

import grails.converters.JSON
import org.springframework.dao.DataIntegrityViolationException

class DealController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list() {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [dealInstanceList: Deal.list(params), dealInstanceTotal: Deal.count()]
    }
	
	def listJSON() {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		def jsonArray = new ArrayList()
		def deals = Deal.list()
		for (deal in deals) {
			def map = new HashMap()
			
			// Set merchant
			def merchant = new HashMap()
			merchant.put("id", deal.merchant.id)
			merchant.put("name", deal.merchant.name)
			
			// Set category
			def category = new HashMap()
			category.put("id", deal.category.id)
			category.put("name", deal.category.name)
		}
		
		
		
		render (text: new JSON(Deal.list()), contentType:"application/json")
	}

    def create() {
        [dealInstance: new Deal(params)]
    }

    def save() {
        def dealInstance = new Deal(params)
        if (!dealInstance.save(flush: true)) {
            render(view: "create", model: [dealInstance: dealInstance])
            return
        }

		flash.message = message(code: 'default.created.message', args: [message(code: 'deal.label', default: 'Deal'), dealInstance.id])
        redirect(action: "show", id: dealInstance.id)
    }

    def show() {
        def dealInstance = Deal.get(params.id)
        if (!dealInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'deal.label', default: 'Deal'), params.id])
            redirect(action: "list")
            return
        }

        [dealInstance: dealInstance]
    }

    def edit() {
        def dealInstance = Deal.get(params.id)
        if (!dealInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'deal.label', default: 'Deal'), params.id])
            redirect(action: "list")
            return
        }

        [dealInstance: dealInstance]
    }

    def update() {
        def dealInstance = Deal.get(params.id)
        if (!dealInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'deal.label', default: 'Deal'), params.id])
            redirect(action: "list")
            return
        }

        if (params.version) {
            def version = params.version.toLong()
            if (dealInstance.version > version) {
                dealInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'deal.label', default: 'Deal')] as Object[],
                          "Another user has updated this Deal while you were editing")
                render(view: "edit", model: [dealInstance: dealInstance])
                return
            }
        }

        dealInstance.properties = params

        if (!dealInstance.save(flush: true)) {
            render(view: "edit", model: [dealInstance: dealInstance])
            return
        }

		flash.message = message(code: 'default.updated.message', args: [message(code: 'deal.label', default: 'Deal'), dealInstance.id])
        redirect(action: "show", id: dealInstance.id)
    }

    def delete() {
        def dealInstance = Deal.get(params.id)
        if (!dealInstance) {
			flash.message = message(code: 'default.not.found.message', args: [message(code: 'deal.label', default: 'Deal'), params.id])
            redirect(action: "list")
            return
        }

        try {
            dealInstance.delete(flush: true)
			flash.message = message(code: 'default.deleted.message', args: [message(code: 'deal.label', default: 'Deal'), params.id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
			flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'deal.label', default: 'Deal'), params.id])
            redirect(action: "show", id: params.id)
        }
    }
}
